/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: javaseapp - Ex03
 * <p>Powered by webrx On 2021-08-12 08:49:30
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Ex03 {
    static Map<String, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        File dir = new File("D:\\ftp");
        //System.out.println(dir.getAbsolutePath());
        count(dir);

        //System.out.println(map);

        System.out.println(map.size() + "种文件");
        map.forEach((k, v) -> System.out.printf("%s = %d%n", k, v));
    }

    public static void count(File f) {
        if (f.isDirectory()) {
            for (File file : f.listFiles()) {
                if (file.isDirectory()) {
                    count(file);
                } else if (file.isFile()) {
                    String name = file.getName();
                    int index = name.lastIndexOf(".");
                    String ext = index == -1 ? name : name.substring(index);
                    if (map.containsKey(ext)) {
                        map.put(ext, map.get(ext) + 1);
                    } else {
                        map.put(ext, 1);
                    }
                }
            }
        }
    }
}
